<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-card shadow="hover" v-show="showSearch" class="mb8">
      <el-form :model="queryParams" ref="queryForm" size="small" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="客户名称" prop="name">
              <el-input
                v-model="queryParams.name"
                placeholder="请输入个人姓名/企业名称"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="企业联系人" prop="contactPerson">
              <el-input
                v-model="queryParams.contactPerson"
                placeholder="请输入企业联系人"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系电话" prop="phone">
              <el-input
                v-model="queryParams.phone"
                placeholder="请输入联系电话"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="地址" prop="address">
              <el-input
                v-model="queryParams.address"
                placeholder="请输入地址"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="客户来源" prop="source">
              <el-select v-model="queryParams.source" placeholder="请选择客户来源" clearable>
                <el-option label="线上" value="1"></el-option>
                <el-option label="线下" value="2"></el-option>
                <el-option label="推荐" value="3"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="auto">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:customer:add']"
          class="mr-2"
        >新增</el-button>
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:customer:edit']"
          class="mr-2"
        >修改</el-button>
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:customer:remove']"
          class="mr-2"
        >删除</el-button>
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:customer:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 数据表格 -->
    <el-card shadow="hover">
      <el-table 
        v-loading="loading" 
        :data="customerList"
        @selection-change="handleSelectionChange"
        style="width: 100%"
        :row-class-name="tableRowClassName"
        border
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="主键" align="center" prop="id" width="80" />
        <el-table-column label="客户类型" align="center" prop="type" width="100">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.type === 1" type="success">个人</el-tag>
            <el-tag v-else-if="scope.row.type === 2" type="primary">企业</el-tag>
            <span v-else>未知</span>
          </template>
        </el-table-column>
        <el-table-column label="客户名称" align="center" prop="name" min-width="180" show-overflow-tooltip />
        <el-table-column label="企业联系人" align="center" prop="contactPerson" min-width="120" show-overflow-tooltip />
        <el-table-column label="联系电话" align="center" prop="phone" min-width="120" show-overflow-tooltip />
        <el-table-column label="地址" align="center" prop="address" min-width="180" show-overflow-tooltip />
        <el-table-column label="客户来源" align="center" prop="source" width="100">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.source === 1" type="info">线上</el-tag>
            <el-tag v-else-if="scope.row.source === 2" type="warning">线下</el-tag>
            <el-tag v-else-if="scope.row.source === 3" type="success">推荐</el-tag>
            <span v-else>未知</span>
          </template>
        </el-table-column>
        <el-table-column label="合作状态" align="center" prop="cooperationStatus" width="100">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.cooperationStatus === 1" type="success">活跃</el-tag>
            <el-tag v-else-if="scope.row.cooperationStatus === 2" type="warning">休眠</el-tag>
            <span v-else>未知</span>
          </template>
        </el-table-column>
        <el-table-column label="是否会员" align="center" prop="isMember" width="100">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.isMember === 1" type="primary">会员</el-tag>
            <el-tag v-else-if="scope.row.isMember === 0" type="default">普通</el-tag>
            <span v-else>未知</span>
          </template>
        </el-table-column>
        <el-table-column label="会员积分" align="center" prop="points" width="100" />
        <el-table-column label="操作" align="center" width="120" fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:customer:edit']"
              class="text-primary hover-btn"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:customer:remove']"
              class="text-danger hover-btn"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <!-- 空状态提示 -->
      <div v-if="!loading && customerList.length === 0" class="empty-state">
        <img src="@/assets/images/empty.svg" alt="暂无数据" class="empty-img">
        <p class="empty-text">暂无客户数据</p>
        <el-button type="primary" size="small" @click="handleAdd">新增客户</el-button>
      </div>
    </el-card>

    <!-- 分页 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
      class="mt-4"
    />

    <!-- 添加或修改客户管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px" size="small">
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="客户类型" prop="type">
              <el-radio-group v-model="form.type">
                <el-radio :label="1">个人</el-radio>
                <el-radio :label="2">企业</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="客户名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入个人姓名/企业名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="企业联系人" prop="contactPerson">
              <el-input v-model="form.contactPerson" placeholder="请输入企业联系人" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入地址" type="textarea" :rows="2" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="客户来源" prop="source">
              <el-select v-model="form.source" placeholder="请选择客户来源">
                <el-option label="线上" value="1"></el-option>
                <el-option label="线下" value="2"></el-option>
                <el-option label="推荐" value="3"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="是否会员" prop="isMember">
              <el-radio-group v-model="form.isMember">
                <el-radio :label="0">普通</el-radio>
                <el-radio :label="1">会员</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="会员积分" prop="points">
              <el-input v-model="form.points" placeholder="请输入会员积分" type="number" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCustomer, getCustomer, delCustomer, addCustomer, updateCustomer } from "@/api/system/customer"
import './style.scss'

export default {
  name: "Customer",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户管理表格数据
      customerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        name: null,
        contactPerson: null,
        phone: null,
        address: null,
        source: null,
        cooperationStatus: null,
        isMember: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        type: [
          { required: true, message: "客户类型不能为空", trigger: "change" }
        ],
        name: [
          { required: true, message: "个人姓名/企业名称不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
        isMember: [
          { required: true, message: "是否会员不能为空", trigger: "change" }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 表格行样式
    tableRowClassName({ row, rowIndex }) {
      return '';
    },
    /** 查询客户管理列表 */
    getList() {
      this.loading = true
      listCustomer(this.queryParams).then(response => {
        this.customerList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        name: null,
        contactPerson: null,
        phone: null,
        address: null,
        source: null,
        cooperationStatus: null,
        isMember: 0,
        createBy: null,
        createTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加客户管理"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getCustomer(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改客户管理"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCustomer(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addCustomer(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除客户"' + (row.name || ids) + '"的数据？').then(function() {
        return delCustomer(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/customer/export', {
        ...this.queryParams
      }, `customer_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>